//
//  CZEmoticonPictureViewCell.swift
//  Weibo27
//
//  Created by apple on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZEmoticonPictureViewCell: UICollectionViewCell {
    // 表情一维数组
    var emoticons: [CZEmoticon]?{
        didSet{
            
            
            // 遍历我们的buttons(隐藏所有button)
            for button in buttons{
                button.hidden = true
            }
            
            
            for (i, emoticon) in emoticons!.enumerate(){
                
                let button = buttons[i]
                // 给我们的自定义button的emoticon的属性赋值
                button.emoticon = emoticon
                // 显示button
                button.hidden = false
                // 代表emoji表情
                if emoticon.isEmoji{
                    // 设置title
                    button.setTitle(((emoticon.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                   // 代表图片表情

                    let path = "\(emoticon.path ?? "")\(emoticon.png ?? "")"
                    let image = UIImage(named: path, inBundle: CZEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                    
                }
                
            }
            
        }
    }
    
    
    // 保存表情按钮数组
    var buttons:[CZEmoticonButton] = [CZEmoticonButton]()
    
    var indexPath: NSIndexPath?{
        didSet{
            
//            countLabel.text = "第\(indexPath?.section ?? 0)组 第\(indexPath?.item ?? 0)页 \n 从0开始"
            
            
            
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  监听方法
    func deleteButtonClick(){
        // 发送通知 告知发布微博控制器 
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteButtonNSNotification, object: nil)
    }
    
    // MARK: - 表情按钮点击
    func buttonClick(button: CZEmoticonButton){
        
        // 发送通知 告知发布微博控制器
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNSNotification, object: button.emoticon)
    }
    
    
    // MARK: -  设置视图
    private func setupUI(){
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addChildButtons()
        // 01 添加控件
        //contentView.addSubview(countLabel)
        
        contentView.addSubview(deleteButton)
        // 02 添加约束
//        countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // button 的宽 高
        let w = (SCREENW - 10) / CGFloat(CZEmoticonMaxCol)
        let h = (216 - 37 - 20) / CGFloat(CZEmoticonMaxRow)
        
        // 设置frame
        // 遍历数组
        for (i, button) in buttons.enumerate(){
            let x = i % CZEmoticonMaxCol
            let y = i / CZEmoticonMaxCol
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
        }
        
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: SCREENW - 5 - w, y: h * 2, width: w, height: h)
        
    }
    
    
    // 添加表情按钮
    func addChildButtons(){
        // 创建button
        for _ in 0..<CZEmoticonMaxCount{
            let button = CZEmoticonButton()
            // 添加事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    
    // MARK: -  懒加载控件
    private lazy var countLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 35)
        lab.textAlignment = .Center
        lab.numberOfLines = 0
        return lab
    }()
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
    
    
}
